//
//  HomePageController.m
//  DoneTask
//
//  Created by NeiQuan on 16/7/27.
//  Copyright © 2016年 Mr-yuwei. All rights reserved.
//

#import "HomePageController.h"
#import "YWStaticPublicHeader.h"
#import "YWHeader.h"
#import "NSData+Base64AES128CBC.h" //有米加密-->烂
@interface HomePageController ()<UIWebViewDelegate,NJKWebViewProgressDelegate>
{
   
    UIWebView                *_webview;
    NJKWebViewProgressView   *_progressView;
    NJKWebViewProgress       *_progressProxy;
    NSDictionary             *_dictionary;
    
}
@end
@implementation HomePageController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title=@"商品详情";
    self.automaticallyAdjustsScrollViewInsets=NO;
    [self.view setBackgroundColor:[ UIColor  colorWithString:@"f2f2f2"]];
    [self makeSubViews];
    [self getNetWorkData];
    //[self openYoumi];
  
}
-(void)openYoumi{//http://w.ymapp.com/wx/ios/lists.html?mt=1&r=加密串
    
    NSString *userid=[NSString stringWithFormat:@"%@",[[ NSUserDefaults standardUserDefaults] objectForKey: @"userID"] ];
    NSString *feedback=@"";
    NSString *useridBaseEncode = [[userid dataUsingEncoding:NSUTF8StringEncoding]base64EncodedString];
    NSString *feedbackBsEnc = [[feedback dataUsingEncoding:NSUTF8StringEncoding]
                               base64EncodedString];
    NSString *encryStri=[NSString stringWithFormat:@"XXXXXXXXXXXXXXXX%@&%@",useridBaseEncode,feedbackBsEnc];
    NSString *appid = @"4a7bc2009b1aba9e";
    // 在有米官网获取的应用 ID
    // 在有米官网获取的应用密钥
    NSString *appSecret = @"84ae319a759a706a";
    NSString *encryString = [[[encryStri dataUsingEncoding: NSUTF8StringEncoding] AES128EncryptWithKey:appSecret] base64EncodedString];
    NSString *r=[appid stringByAppendingString:encryString];
    r=[self urlencode:r];
    NSString *finalString = [NSString stringWithFormat:@"http://w.ymapp.com/wx/ios/lists.html?mt=1&ht=0&r=%@",r]; NSLog(@"%@",finalString);
    
       [[ UIApplication sharedApplication] openURL:[ NSURL URLWithString:finalString]];
    

}
-(NSString*)urlencode:(NSString*)OPSString
{
      NSString* escapedUrlString= (NSString*) CFBridgingRelease(CFURLCreateStringByAddingPercentEscapes(NULL,(CFStringRef)OPSString,NULL, (CFStringRef)@"!*'();:@&=+$,/?%#[]", kCFStringEncodingUTF8 ));
    return escapedUrlString;
}
-(void)viewWillAppear:(BOOL)animated{
    [ super viewWillAppear:animated];
    
    for (UITapGestureRecognizer *tap in self.view.gestureRecognizers)
    {
        tap.enabled=NO;
        
    }
}
-(void)viewWillDisappear:(BOOL)animated{
    [ super viewWillDisappear:animated];
    [_progressView removeFromSuperview];
    [ self removeMBProgressView];
    for (UITapGestureRecognizer *tap in self.view.gestureRecognizers)
    {
        tap.enabled=YES;
        
    }
    
}
-(void)makeSubViews{

    UIWebView *webview=[[ UIWebView alloc] initWithFrame:CGRectMake(0, 0, YWWidth, YWHeight-64)];
    [webview setBackgroundColor:[ UIColor clearColor]];
    _webview=webview;
    _progressProxy = [[NJKWebViewProgress alloc] init];
    webview.delegate = _progressProxy;
    _progressProxy.webViewProxyDelegate = self;
    _progressProxy.progressDelegate = self;
    
    CGFloat progressBarHeight = 2.f;
    CGRect barFrame = CGRectMake(0, 46 - progressBarHeight,YWWidth, progressBarHeight);
    _progressView = [[NJKWebViewProgressView alloc] initWithFrame:barFrame];
    [_progressView.progressBarView setBackgroundColor:[ UIColor colorWithString:@"#F4AE25"]];
    _progressView.progress=0.0;
    _progressView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleTopMargin;
   [self.navigationController.navigationBar addSubview:_progressView];
    [self.view addSubview:webview];
    
}
#pragma mark --private Method--获取商品详情
-(void)getNetWorkData
{
   [self showMBProgressHUDLoadinginView:self.view];
    NSDictionary *parameters=@{@"id":_neswID};
    [NetWorkRequest netWorkPOSTWithURLString:@"goods/queryDetail" parameters:parameters SuccessBlock:^(NSDictionary *dic)
     {
         [self removeMBProgressView];
         if ([dic[@"code"]integerValue]==0)
         {
        _dictionary=dic[@"response"];
        [_webview loadRequest:[NSURLRequest requestWithURL: [ NSURL URLWithString:[ NSString stringWithFormat:@"https://item.taobao.com/item.htm?spm=a219e.1191392.1111.13.lTCkmf&id=%@&scm=1029.newlist-0.1.50002766&ppath=&sku=&ug=#detail",_dictionary[@"taobaoGoodsId"]]]]];
            
         }
     } failBlock:^(NSError *error)
     {
         [self removeMBProgressView];
     }];

}
-(void)clickrefresh{
    
     [self removeMBProgressView];
}
- (void)webViewProgress:(NJKWebViewProgress *)webViewProgress updateProgress:(float)progress{
    
    [_progressView setProgress:progress animated:YES];
    
}
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType{
    
    return YES;
}
- (void)webViewDidStartLoad:(UIWebView *)webView{
    
    [self showMBProgressHUDLoadinginView:self.view ];
    
}
- (void)webViewDidFinishLoad:(UIWebView *)webView{
    
    [self removeMBProgressView];
    
}
- (void)webView:(UIWebView *)webView didFailLoadWithError:( NSError *)error{
    
    [self removeMBProgressView];
    
   // [self showMBProgressHUDNetErrorMessage:@"加载失败,请查看你的网络" inView:self.view];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
